package cn.jarlen.richcommon2.shapeimageview;

import android.content.Intent;
import android.view.View;

import com.bumptech.glide.Glide;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.Random;

import cn.jarlen.richcommon.ui.BaseActivity;
import cn.jarlen.richcommon.widget.CircleImageView;
import cn.jarlen.richcommon2.R;

/**
 * Created by jarlen on 2017/8/30.
 */

public class TestCircleImageActivity extends BaseActivity implements View.OnClickListener {

    private CircleImageView circleImageView;

    private String url;

    public static String[] urls = {
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1504710764&di=dc7772dbd9ec5664c47029221656c439&imgtype=jpg&er=1&src=http%3A%2F%2Fwww.zhlzw.com%2FUploadFiles%2FArticle_UploadFiles%2F201204%2F20120412123914329.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1504116101035&di=36b5877ac9005fdfe2fd7fad2a58ada2&imgtype=0&src=http%3A%2F%2Fimage.tianjimedia.com%2FuploadImages%2F2014%2F354%2F11%2FN4GGEA982702.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1504116157962&di=6adfcab2526abab54e41e8134a7446a9&imgtype=0&src=http%3A%2F%2Fimg02.tooopen.com%2Fimages%2F20160614%2Ftooopen_sy_165053819689.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1504116194658&di=c6fbc580710a633974b835a8fb32776f&imgtype=jpg&src=http%3A%2F%2Fimg0.imgtn.bdimg.com%2Fit%2Fu%3D2071759339%2C81190912%26fm%3D214%26gp%3D0.jpg",
            "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1640891221,3300482097&fm=26&gp=0.jpg"
    };

    @Override
    protected int getLayoutId() {
        return R.layout.activity_test_shape_imageview;
    }

    @Override
    protected void onBindView() {
        EventBus.getDefault().register(this);
        circleImageView = (CircleImageView) findViewById(R.id.iv_circle);
        findViewById(R.id.testBtn).setOnClickListener(this);
        findViewById(R.id.jumpBtn).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.testBtn:
                int nums = new Random().nextInt(4);
                url = urls[nums];
                EventBus.getDefault().post(new CircleUpdateEvent(url));
                break;
            case R.id.jumpBtn:
                Intent intent = new Intent(this, TestCircleImageView2Activity.class);
                intent.putExtra("url", url);
                this.startActivity(intent);
                break;
            default:

                break;
        }
    }

    @Subscribe
    public void updateCircle(CircleUpdateEvent event) {
        url = event.getUrl();
        Glide.with(this)
                .load(url)
                .fitCenter()
                .placeholder(R.drawable.shape_img_default)
                .into(circleImageView);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
